//
//  IncomeListViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/24.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "IncomeListViewController.h"
#import "IncomeListTableViewCell.h"

@interface IncomeListViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UILabel *incomeLab;

@property (nonatomic, strong) NSMutableArray < IncomeListModel *> * dataArray;

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;

@end

@implementation IncomeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self initParam];
    [self request_IncomeListData];
    
}

- (void)initParam {
    self.pageNum = 1;
    self.pageSize = 100;
}

- (void)request_IncomeListData {
    
    NSDictionary *dic = @{}.mutableCopy;
    [dic setValue:@(self.pageNum) forKey:@"curPage"];
    [dic setValue:@(self.pageSize) forKey:@"pageSize"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_POPULARIZE_RECORD];
    
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    
                    //余额
                    double  bounty = [[resultDic valueForKey:@"bounty"] doubleValue];
                    
                    self.incomeLab.text  = [NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:bounty]];
                    
                    //收益列表
                    if (self.pageNum == 1) {
                        [self.dataArray removeAllObjects];
                    }
                    

                    
                    NSArray *rewardRes = [resultDic valueForKey:@"rewardRes"];
                    
                    if ( [rewardRes isKindOfClass:[NSArray class]] && rewardRes.count < self.pageSize) {
                        [self.tableview.mj_footer endRefreshingWithNoMoreData];
                    }
                    
                    for (int i = 0; i < rewardRes.count; i++) {
                        
                        NSDictionary *rewardDic = rewardRes[i];
                        IncomeListModel *model = [[IncomeListModel alloc] init];
                        
                        double time = [[rewardDic valueForKey:@"createTime"] doubleValue];
                        model.itemTime = [Utilities getTimeFromTimestamp:time];
                        model.itemIncome = [[rewardDic valueForKey:@"acquirableMoney"] doubleValue];
                        
                        //0初级场 1正式场收益 2推荐用户收益 3团队极差收益 4终极场收益
                        NSString *rewardType = [rewardDic valueForKey:@"rewardType"];
                        
                        if (![rewardType isKindOfClass:[NSString class]]) {
                            rewardType = [NSString stringWithFormat:@"%@",rewardType];
                        }
                        
                        
                        if ([rewardType isEqualToString:@"0"]) {
                            model.itemTitle = @"初级场收益";
                            
                        } else if ([rewardType isEqualToString:@"1"]) {
                            model.itemTitle = @"正式场收益";

                        } else if ([rewardType isEqualToString:@"2"]) {
                            model.itemTitle = @"推荐用户收益";

                        } else if ([rewardType isEqualToString:@"3"]) {
                            model.itemTitle = @"终极场收益";

                        } else if ([rewardType isEqualToString:@"4"]) {
                            model.itemTitle = @"一拍必得";
                            
                        } else if ([rewardType isEqualToString:@"5"]) {
                            model.itemTitle = @"商城拍场收益";
                        } else {
                            model.itemTitle = @"未知收益类型，请升级版本查看";
                        }
                        
                        [self.dataArray addObject:model];
                    }
                }
                
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        } else {
            
        }
        
        [self.tableview reloadData];
        [[TTNoResultManager shareManager] hideNoResultWithView:self.view];
        

        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        WEAKSELF
        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.view errorCode:error.code btnTitle:@"重新加载" didPressButton:^{
            [weakSelf request_IncomeListData];
        }];
    }];
}


- (void)initView {
    
    self.title = @"累计收益";
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"IncomeListTableViewCell" bundle:nil] forCellReuseIdentifier:@"IncomeListTableViewCellID"];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    WEAKSELF
    self.tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [weakSelf initParam];
        [weakSelf request_IncomeListData];
    }];
    
    self.tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        weakSelf.pageNum ++;
        [weakSelf request_IncomeListData];
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    IncomeListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IncomeListTableViewCellID" forIndexPath:indexPath];
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}


- (NSMutableArray<IncomeListModel *> *)dataArray {
    
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}
@end
